<?php

namespace App\Console\Commands;

use App\Models\RechargeOrder;
use App\Models\RechargeTask;
use App\Models\Team;
use Illuminate\Console\Command;

class ShareCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'share:team {--userid=}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $userid = $this->option('userid');
        if ($userid){
            $team = Team::where("user_id",$userid)->where("role","leader")->get();
        }else{
            $team = Team::where("role","leader")->get();
        }
        if ($team->count()){
            foreach ($team as $item){
                //查询团员
                $teamUser = Team::where("team_code",$item['team_code'])->where("role","member")->get();
                foreach ($teamUser as $value) {
                    //查询充值订单
                    $rechargeData = RechargeOrder::where("user_id",$value['user_id'])->where("status",">",0)->count();
                    if ($rechargeData>0){
                        $value->is_recharge = 1;
                        $value->remark = "有效";
                        $value->save();
                    }else{
                        $value->is_recharge = 0;
                        $value->remark = "未充值";
                        $value->save();
                    }
                }
            }
        }
    }
}
